﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using DevExpress.Xpo;
using Zero.Core.Domain;
using Zero.Core.Domain.Query;
using Zero.DataLayer.Donnees;
using Zero.POCO.Enum.Helper.Support;

namespace Zero.DataLayer.Repository
{
    public class SupportPresseRepository : IReadOnlyRepository<SupportPresse,int>
    {
        public SupportPresse FindBy(int id)
        {

            var config = new AppSettingsReader();
            using (
                var uow = new UnitOfWork()
                {
                    ConnectionString =
                        ((string)config.GetValue("connect", typeof(string)))
                })
            {
                var instance = uow.GetObjectByKey<XpoSupportPresse>(id);
                if(instance==null)
                return null;
                return MAP.MapInverse.MapSupportPresse(instance);
            }
        }

        public IEnumerable<SupportPresse> FindAll()
        {
            List<SupportPresse> Model;

            var config = new AppSettingsReader();
            using (
                 var uow = new UnitOfWork()
                 {
                     ConnectionString =
                         ((string)config.GetValue("connect", typeof(string)))
                 })
            {
                var Instances = new XPCollection<Zero.DataLayer.Donnees.XpoSupportPresse>(uow);
                Model = Instances.Select(Instance => MAP.MapInverse.MapSupportPresse(Instance)).ToList();
            }
            
            return Model;
        }

        public IEnumerable<SupportPresse> FindBy(Query query)
        {
            List<SupportPresse> Model;
            var firstOrDefault = query.Criteria.FirstOrDefault();
            if (firstOrDefault != null && firstOrDefault.criteriaOperator == CriteriaOperator.Equal)
            {

                var config = new AppSettingsReader();
                using (
                var uow = new UnitOfWork()
                {
                    ConnectionString =
                        ((string)config.GetValue("connect", typeof(string)))
                })
                {var instances = new XPCollection<XpoSupportPresse>(uow);
                    IEnumerable<XpoSupportPresse> Xpoinstances;
                    var orDefault = query.Criteria.FirstOrDefault();
                    Xpoinstances = orDefault != null && orDefault.PropertyName=="Libelle" ? instances.Where(p =>
                    {
                        var criterion = query.Criteria.FirstOrDefault();
                        return criterion != null && p.Libelle == criterion.Value.ToString();
                    }) : instances.Where(p =>
                    {
                        var @default = query.Criteria.FirstOrDefault();
                        return @default != null && p.Code.ToString() == @default.Value.ToString();
                    });
                    Model = new List<SupportPresse>();
                    foreach (var individu in Xpoinstances)
                    {
                        Model.Add(MAP.MapInverse.MapSupportPresse(individu));
                    }
                }

                return Model;
            }
            return null;
        }

        public IEnumerable<SupportPresse> FindBy(Query query, int index, int count)
        {
            throw new NotImplementedException();
        }
    }




    public  class RepositoryPresseCache : IReadOnlyRepository<SupportPresse, int>
    {
        private static IEnumerable<SupportPresse> instances;
        public SupportPresse FindBy(int id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<SupportPresse> FindAll()
        {
            throw new NotImplementedException();
        }

        public  IEnumerable<SupportPresse> FindBy(Query query)
        {
            if (instances == null)
                instances = new SupportPresseRepository().FindAll();


            var firstOrDefault = query.Criteria.FirstOrDefault();
            var list = firstOrDefault != null && firstOrDefault.PropertyName == "Libelle" ? instances.Where(p =>
            {
                var orDefault = query.Criteria.FirstOrDefault();
                return orDefault != null && p.Libelle == orDefault.Value.ToString();
            }) : instances.Where(p =>
            {
                var criterion = query.Criteria.FirstOrDefault();
                return criterion != null && p.Code.ToString() == criterion.Value.ToString();
            });
            return list;
        }

        public IEnumerable<SupportPresse> FindBy(Query query, int index, int count){
            throw new NotImplementedException();
        }
    }
}
